#include "mpp_display_dev.h"

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <xf86drm.h>
#include <xf86drmMode.h>
#include <iostream>
using namespace std;
mpp_display_dev::mpp_display_dev(string path):
                                                                    fd(-1)                                                                   
{
    int ret = 0, i = 0, j=0;
   
    
    this->connectors.clear();
    this->encoders.clear();
    this->crtcs.clear();    

    this->fd = open(path.c_str(), O_RDWR | O_CLOEXEC);
    if ( this->fd < 0) {
        printf("failed to open card0\n");
        this->fd  = -1;
        return ;
    }

    ret = drmSetClientCap(this->fd, DRM_CLIENT_CAP_ATOMIC, 1);
    if (ret) {
        printf("failed to set client cap atomic\n");
        goto err;
    }
    ret = drmSetClientCap(this->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
    if (ret) {
        printf("failed to set client cap\n");
        goto err;
    }

    
    return ;
    err:
        distroy_all();
}


int mpp_display_dev::mpp_display_init_element()
{
    int ret = 0, i = 0, j=0;
    drmModeRes* res = NULL;


     /*这边拿到DRM的DRTC(扫描器)，encoders(编码器）connectors(连接器)*/
    res = drmModeGetResources(this->fd);
    if (!res) {
        printf("failed to get r\n");
        goto err;
    }


    //--拿到DRM三大子模块的数量


    
    //this->count_connectors = res->count_connectors;
    printf("连接器数量%d \n",res->count_connectors);
    this->connectors = mpp_element_create_connect(this->fd,res->count_connectors,res->connectors);
    if(this->connectors.size() == 0){
        cout<<"获取连接器失败"<<endl;
        goto err;
    }

    //this->count_encoders = res->count_encoders;
    this->encoders = mpp_element_create_encoder(this->fd,res->count_encoders,res->encoders);
    if(this->encoders.size() == 0){
        cout<<"获取编码器失败"<<endl;
        goto err;
    }

    

    //this->count_crtcs = res->count_crtcs;
    this->crtcs = mpp_element_create_crtc(this->fd, res->count_crtcs,res->crtcs);
    if(this->crtcs.size() == 0){
        cout<<"获取编码器失败"<<endl;
        goto err;
    }

    for (i = 0; i < this->connectors.size() == 0; i++) {

    }
    printf("设备初始化完毕   连接器数量:%d 编码器数量:%d 扫描器数量:%d\n", this->connectors.size(),this->encoders.size(),this->crtcs.size());
    return 0;




    err:
       //distroy_all();
    return -1;
}
void mpp_display_dev::distroy_all()
{
    printf("销毁各个子模块\n");
    mpp_element_distroy_crtc(this->fd, this->crtcs);  
    mpp_element_distroy_encoder(this->fd,this->encoders);    
    mpp_element_distroy_connect(this->fd,this->connectors);
    
      
    if(this->fd > 0){
        close(this->fd);
        this->fd = -1;
    }
        
}
mpp_display_dev:: ~mpp_display_dev()
{
    distroy_all();
}
int mpp_display_dev::mpp_display_dev_setCrtc(uint32_t crtcId, uint32_t bufferId,
                                                uint32_t x, uint32_t y, uint32_t *connectors, int count,
                                        drmModeModeInfoPtr mode)
{
    return drmModeSetCrtc(this->fd, crtcId,
                             bufferId, x, y, connectors,
                             count, mode);
}
int mpp_display_dev::mpp_display_dev_get_fd()
{
    return this->fd;
}

vector<drmModeConnectorPtr> mpp_display_dev::mpp_dev_get_connectors()
{
    return this->connectors;
}
vector<drmModeEncoderPtr> mpp_display_dev::mpp_dev_get_encoders()
{
    return this->encoders;
}
vector<drmModeCrtcPtr>  mpp_display_dev::mpp_dev_get_crtcs()
{
    return this->crtcs;
}




